/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.txweb.dao.impl;

import com.jspx.sober.TableModels;
import com.jspx.sober.jdbc.JdbcOperations;
import com.jspx.sober.criteria.expression.Expression;
import com.jspx.sober.criteria.Order;
import com.jspx.sober.util.DataMap;
import com.jspx.txweb.table.PostCalendar;
import com.jspx.utils.DateUtil;
import com.jspx.txweb.dao.PostCalendarDAO;
import com.jspx.utils.ObjectUtil;
import com.jspx.utils.StringUtil;

import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: 陈原
 * Date: 2007-10-6
 * Time: 15:08:15
 * 记录软件发布数量,详细到天(几号),updatePostCalendar() 将更新，每次加1
 */
public class PostCalendarDAOImpl extends JdbcOperations implements PostCalendarDAO
{
    private String namespace;

    public String getNamespace()
    {
        return namespace;
    }

    public void setNamespace(String namespace)
    {
        this.namespace = namespace;
    }

    /**
     *
     * @param beginDate  开始日期
     * @param endDate  结束日期
     * @return 得到两个日期间的数据列表
     */
    @SuppressWarnings("unchecked")
    public List<PostCalendar> getBetweenList(Date beginDate, Date endDate)
    {
        return (List<PostCalendar>) createCriteria(PostCalendar.class).add(Expression.between("postDate", beginDate, endDate))
                .add(Expression.eq("namespace", namespace)).addOrder(Order.asc("postDate")).list(false);

    }

    /**
     *
     * @param date  日期
     * @return  得到某一天的提交数据
     */
    @SuppressWarnings("unchecked")
    public PostCalendar getPostCalendar(Date date)
    {
        return (PostCalendar) createCriteria(PostCalendar.class).add(Expression.eq("shortDate", DateUtil.toString(date,DateUtil.DAY_FORMAT))).add(Expression.eq("namespace", namespace)).objectUniqueResult(false);
    }

    /*
     *
     * @param year 年
     * @return 返回一年的所有月份提交数据
     */
    @SuppressWarnings("unchecked")
    public Map<String,Integer> getMonthListCountMap(int year)
    {
        TableModels soberTable = getSoberTable(PostCalendar.class);
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT dateMonth,sum(postCount) as NUM FROM ").append(soberTable.getTableName());
        sql.append(" WHERE dateYear>=").append(year).append("AND namespace=\'").append(StringUtil.toScript(namespace)).append("\'");
        sql.append(" GROUP BY dateMonth ORDER BY dateMonth DESC");
        List<DataMap<String, Object>> list = queryForList(sql.toString(),null,1,5000);
        Map<String,Integer> result = new HashMap<String,Integer>();
        for (Map<String, Object> map:list)
        {
            if (map == null) continue;
            result.put(year + "-" + map.get("dateMonth"), ObjectUtil.toInt(map.get("NUM")));
        }
        return result;
    }

    public boolean updatePostCalendar() throws Exception
    {
       return updatePostCalendar(new Date(),1);
    }

    /**
     *
     * @param date   日期
     * @param count  数量
     * @return  更新日历统计数据
     */
    public boolean updatePostCalendar(Date date, long count) throws Exception
    {
        PostCalendar postCalendar = getPostCalendar(date);
        if (postCalendar == null)
        {
            postCalendar = new PostCalendar();
            postCalendar.setPostCount(count);
            postCalendar.setPostDate(date);
            postCalendar.setNamespace(namespace);
            save(postCalendar);
        } else if (postCalendar.getPostCount() != count)
        {
            postCalendar.setPostCount(postCalendar.getPostCount() + count);
            postCalendar.setPostDate(date);
            postCalendar.setNamespace(namespace);
            update(postCalendar);
        }
        return true;
    }

}